﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Kovan.Model.Parameter.UserCenter;
using Kovan.BLL.UserCenter;
using Kovan.Model.Entity.UserCenter;

namespace Kovan.UI.Areas.Manage.Controllers
{
    public class ApplicationController : ManageControllerBase
    {
        //
        // GET: /Manage/Application/

        public ActionResult Index(PApplication param)
        {
            if (!(param.PageSize > 0))
            {
                param.PageSize = 20;
            }
            if (!(param.PageIndex > 0))
            {
                param.PageIndex = 1;
            }
            var items = BActApplication.GetInstances().GetList(param);
            ViewBag.Param = param;
            return View("index", items);
        }

        public ActionResult GetOperate(int? id)
        {
            act_application model = new act_application();
            if (id.HasValue)
            {
                model = BActApplication.GetInstances().GetModel(id.Value);
            }
            return View("OperateDialog", model);
        }

        public ContentResult Delete(string ids)
        {
            var statuscode = string.Empty;
            List<string> idss = ids.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
            var result = BActApplication.GetInstances().BatchDelete(idss);
            if (result > 0)
            {
                statuscode = GetSucceedCode("保存成功", "application_manage", Url.Content("~/Manage/Application"), "应用程序管理");
            }
            return Content(statuscode);
        }

        public ActionResult SaveApplication(act_application model)
        {
            int result = 0;
            string statuscode = string.Empty;
            if (model.application_id.HasValue)
            {
                var model1 = BActApplication.GetInstances().GetModel(model.application_id.Value);
                model1.name = model.name;
                model1.description = model.description;
                model1.url = model.url;
                model1.icon_url = model.icon_url;
                model1.test_url = model.test_url;
                

                result = BActApplication.GetInstances().Update(model1);
            }
            else
            {
                result = BActApplication.GetInstances().Add(model);
            }

            if (result > 0)
            {
                //成功
                statuscode = GetSucceedCode("保存成功", "application_manage", Url.Content("~/Manage/Application"), "应用程序管理");
            }
            else
            {
                //失败
                statuscode = GetFailCode("保存失败", "", "", "");
            }
            return Content(statuscode);
        }

    }
}
